package com.rxyl.love.petdoctor.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.rxyl.love.petdoctor.R;
import com.rxyl.love.petdoctor.acticity.CityActivity;
import com.rxyl.love.petdoctor.acticity.DoctorDetailActivity;
import com.rxyl.love.petdoctor.acticity.SearchActivity;
import com.rxyl.love.petdoctor.adapter.FindDoctorAdapter;
import com.rxyl.love.petdoctor.constant.Urls;
import com.rxyl.love.petdoctor.entity.CityEntity;
import com.rxyl.love.petdoctor.entity.FindDoctor;
import com.rxyl.love.petdoctor.util.NetworkUtil;
import com.rxyl.love.petdoctor.util.SharedUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/1/9 0009.
 */
public class DoctorFragment extends Fragment implements View.OnClickListener {
    private RelativeLayout rl_level, rl_filter, rl_city;
    private TextView tv_level, tv_filter, tv_city;
    private ImageView iv_filter, iv_level ,iv_doctor_search;
    private PopupWindow popupWindow, popupWindow2;
    private View popupView, popupView2;
    private View view;
    private RadioGroup rg_level, rg_filter;
    private ListView listView;

    private List<FindDoctor.DataEntity.ListEntity> datas = new ArrayList<>();
    private FindDoctorAdapter findDoctorAdapter;

    public static final int REQUEST_CODE = 0x001;
    public static final int RESULET_CODE = 0x002;
    private String url;
    private int cityId = 2;

    private FindDoctor.DataEntity.ListEntity entity;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.doctor_fragment_layout, container, false);
        init();
        doctorLevel();
        doctorFilter();
        rl_city.setOnClickListener(this);
        loadDatas();
        return view;
    }

    private void init() {
        rl_level = (RelativeLayout) view.findViewById(R.id.rl_doctor_level);
        tv_level = (TextView) view.findViewById(R.id.tv_doctor_district);
        iv_level = (ImageView) view.findViewById(R.id.iv_doctor_district);
        rl_filter = (RelativeLayout) view.findViewById(R.id.rl_doctor_filter);
        tv_filter = (TextView) view.findViewById(R.id.tv_doctor_filter);
        iv_filter = (ImageView) view.findViewById(R.id.iv_doctor_filter);
        rl_city = (RelativeLayout) view.findViewById(R.id.rl_select_city);
        tv_city = (TextView) view.findViewById(R.id.tv_doctor_city);
        listView = (ListView) view.findViewById(R.id.lv_doctor_list);
        iv_doctor_search = (ImageView) view.findViewById(R.id.iv_doctor_search);

        String cityname = SharedUtil.getString("cityname");
        int cityid = SharedUtil.getInt("cityid");
        if (cityname != null && cityid != -1) {
            this.cityId = cityid;
            tv_city.setText(cityname);
        }

        findDoctorAdapter = new FindDoctorAdapter(getActivity(), datas);
        listView.setAdapter(findDoctorAdapter);
        //跳转医生信息详情页面
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(getActivity(), DoctorDetailActivity.class);
                int docId=datas.get(position).getUid();
                intent.putExtra("docId",docId);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });

    }

    //选择医生的级别
    private void doctorLevel() {
        popupView = View.inflate(getActivity(), R.layout.popupwindow_doctor_select, null);
        popupWindow = new PopupWindow(popupView,
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                true);
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        popupWindow.setOutsideTouchable(true);
        popupWindow.setTouchable(true);

        //设置医生级别的RadioGroup的点击事件
        rg_level = (RadioGroup) popupView.findViewById(R.id.rg_doctor_select);
        rg_level.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rbtn_doctor_all:
                        tv_level.setText("全部");
                        popupWindow.dismiss();
                        SharedUtil.putInt("job", 0);
                        loadDatas();
                        break;
                    case R.id.rbtn_doctor_attending:
                        tv_level.setText("主治医师");
                        popupWindow.dismiss();
                        SharedUtil.putInt("job", 1);
                        loadDatas();
                        break;
                    case R.id.rbtn_doctor_chief:
                        tv_level.setText("主任医生");
                        popupWindow.dismiss();
                        SharedUtil.putInt("job", 2);
                        loadDatas();
                        break;
                    case R.id.rbtn_doctor_veterianrian:
                        tv_level.setText("执业兽医师");
                        popupWindow.dismiss();
                        SharedUtil.putInt("job", 3);
                        loadDatas();
                        break;
                    case R.id.rbtn_doctor_veterinary:
                        tv_level.setText("执业助理兽医师");
                        popupWindow.dismiss();
                        SharedUtil.putInt("job", 4);
                        loadDatas();
                        break;
                }
            }
        });

        //弹窗隐藏监听
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                tv_level.setTextColor(Color.rgb(85, 85, 85));
                iv_level.setImageResource(R.mipmap.search_down);
            }
        });

        //弹窗显示的点击事件
        rl_level.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (popupWindow.isShowing()) {
                    popupWindow.dismiss();
                }
                popupWindow.showAsDropDown(rl_level);
                tv_level.setTextColor(Color.rgb(29, 153, 246));
                iv_level.setImageResource(R.mipmap.search_up);
            }
        });
        //搜索页面
        iv_doctor_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), SearchActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });
    }

    private void doctorFilter() {
        popupView2 = View.inflate(getActivity(), R.layout.popupwindow_doctor_filter, null);
        popupWindow2 = new PopupWindow(popupView2,
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                true);
        popupWindow2.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        popupWindow2.setOutsideTouchable(true);
        popupWindow2.setTouchable(true);

        //设置医生智能选择的RadioGroup的点击事件
        rg_filter = (RadioGroup) popupView2.findViewById(R.id.rg_doctor_filter);
        rg_filter.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rbtn_doctor_recommend:
                        tv_filter.setText(R.string.recommend);
                        popupWindow2.dismiss();
                        SharedUtil.putInt("sort", 1);
                        loadDatas();
                        break;
                    case R.id.rbtn_doctor_evaluate:
                        tv_filter.setText(R.string.evaluate);
                        popupWindow2.dismiss();
                        SharedUtil.putInt("sort", 3);
                        loadDatas();
                        break;
                    case R.id.rbtn_doctor_distance:
                        tv_filter.setText(R.string.distance);
                        popupWindow2.dismiss();
                        SharedUtil.putInt("sort", 2);
                        loadDatas();
                        break;
                }
            }
        });

        popupWindow2.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                tv_filter.setTextColor(Color.rgb(85, 85, 85));
                iv_filter.setImageResource(R.mipmap.search_down);
            }
        });

        //智能选择弹窗显示的点击事件
        rl_filter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (popupWindow2.isShowing()) {
                    popupWindow2.dismiss();
                }
                popupWindow2.showAsDropDown(rl_filter);
                tv_filter.setTextColor(Color.rgb(29, 153, 246));
                iv_filter.setImageResource(R.mipmap.search_up);
            }
        });
    }


    @Override
    public void onClick(View v) {
        startActivityForResult(new Intent(getActivity(),
                CityActivity.class), REQUEST_CODE);
    }


    //返回城市信息
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE && resultCode == RESULET_CODE) {
            CityEntity cityEntity = (CityEntity) data.getSerializableExtra("citys");
            cityId = cityEntity.getCity_id();
            tv_city.setText(cityEntity.getCity_name());
            SharedUtil.putString("cityname", cityEntity.getCity_name());
            SharedUtil.putInt("cityid", cityId);
            loadDatas();
        }
    }

    private String city = "深圳";
    private int sort = 1;
    private int page = 1;
    private int job = 0;

    /**
     * 加载数据
     * api.5ichong.com/v1.2/doctor/find_doctor_with_list?
     * city=%E6%B7%B1%E5%9C%B3%E5%B8%82
     * latitude=22.534743
     * limit=20
     * sort=1
     * page=1
     * longitude=113.944708
     */
    private void loadDatas() {
        city = SharedUtil.getString("cityname");
        sort = SharedUtil.getInt("sort");
        job = SharedUtil.getInt("job");
        url = Urls.FIND_DOCTORS + "?city=" + city + "&latitude=22.534743&limit=20&sort=" + sort +
                "&page=" + page + "&job=" + job + "&longitude=113.944708";
        NetworkUtil.getInstance().getFindDoctor(url, new RequestCallBack() {
            @Override
            public void onSuccess(ResponseInfo responseInfo) {
                Gson gson = new Gson();
                FindDoctor findDoctor = gson.fromJson(responseInfo.result.toString(),
                        FindDoctor.class);

                if (page == 1) {
                    datas.clear();
                }
                if (findDoctor != null && findDoctor.getData() != null) {
                    datas.addAll(findDoctor.getData().getList());
                    findDoctorAdapter.notifyDataSetChanged();
                }

            }

            @Override
            public void onFailure(HttpException error, String msg) {

            }
        });
    }
}
